/**   
 * @Title: Mysql_DB.java 
 * @Package: com.xd100.function.db.impl 
 * @Description: 
 * @author: xiaoyu Mo (liujun)   
 * @date: 2014-3-25 下午4:05:55  
 */
package com.xd100.lzll.gm.connection.impl;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.dbutils.DbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.xd100.lzll.gm.cache.GmCache;
import com.xd100.lzll.gm.connection.DBIF;
import com.xd100.lzll.gm.connection.connectionobject.GmConfigPO;

@Component("dBConnection")//代表中立的一些类  不写括号的 默认类名 首字母小写 
public class DBConnection implements DBIF {
	
	@Autowired
	@Qualifier("jdbctemplate")
	private JdbcTemplate jdbctemplate;
	
	/* (非 Javadoc) 
	* <p>Title: getTemplate</p> 
	* <p>Description: </p> 
	* @param type
	* @param serverId
	* @return
	* @throws Exception 
	* @see com.xd100.function.db.DBIF#getTemplate(java.lang.String, java.lang.String) 
	*/
	@Override
	public  Object getTemplate(String type, String serverId) throws Exception {
		try{
			if(DBConstant.LOCAl_JDBCTEMPLATE.equals(type)){//本地gm库
				return jdbctemplate;
			} else {
				//从缓存中获取信息
				Map<String, GmConfigPO> gmConfigPO_Map = GmCache.gmConfigPO_Map;
				GmConfigPO gmConfig = gmConfigPO_Map.get(serverId);
				if (DBConstant.GAME_DATABASE.equals(type)) {//游戏库
					return this.getMysqlConnection(gmConfig.getLog_gs_url(), gmConfig.getLog_gs_usr(), gmConfig.getLog_gs_pwd(), type);
				} else if (DBConstant.LOG_DATABASE.equals(type)) {// 日志库
					return this.getMysqlConnection(gmConfig.getLog_sql_url(), gmConfig.getLog_sql_usr(), gmConfig.getLog_sql_pwd(), type);
				} else if (DBConstant.LOGIN_DATABASE.equals(type)) {// 登录库
					return this.getMysqlConnection(gmConfig.getLogin_sql_url(), gmConfig.getLogin_sql_usr(), gmConfig.getLogin_sql_pwd(), type);
				} else if (DBConstant.PAY_DATABASE.equals(type)) {// 充值库
					return this.getMysqlConnection(gmConfig.getPay_sql_url(), gmConfig.getPay_sql_usr(), gmConfig.getPay_sql_pwd(), type);
				}
			}
		}catch(Exception e){
			e.getStackTrace();
			throw new Exception(e.getMessage(),e);
		}
		return null;
	}

	/**
	 * @Title: getMysqlConnection
	 * @Description: 根据url 用户 密码 得到数据库连接
	 * @param: @param url
	 * @param: @param usr
	 * @param: @param pwd
	 * @param: @param type
	 * @param: @throws GmException 
	 * @return: java.sql.Connection 返回类型
	 */
	private java.sql.Connection getMysqlConnection(String url, String usr, String pwd, String type) throws Exception {
		try {
			String jdbcDriver = "com.mysql.jdbc.Driver";
			DbUtils.loadDriver(jdbcDriver);
			DriverManager.setLoginTimeout(5000);
			return DriverManager.getConnection(url, usr, pwd);
		} catch (SQLException e) {
			System.out.println("url=" + url);
			System.out.println("usr=" + usr);
			System.out.println("pwd=" + pwd);
			throw new Exception(type + "-->ConnUtil--getConnection SQLException");
		}
	}
}
